$(document).ready(function() {
   
    
  function changePrice(price,period)
  {
    var company_discount = 0,discount = 0;
    if($('.discount').text() != "")
    {
      company_discount = parseInt($('.discount').text());
    }
    if($.isNumeric($('.get-discount').val()))
    {
      discount = $('.get-discount').val();
    }
       
    current = price*period;
    $('.total_price').text(current);
    current = current - (current*(discount/100));
    current = current - (current*(company_discount/100));
    
    $('.final_price').text(current);
    $('#final_price').val(current);
    $('.credit').text("-"+current);  
    
  }
    
  $('.company-select').change(function(){
    if ($('.company-select').is(':checked')) {
      $('#getCompanies').submit();
    }
    else
    {
      window.location="customers.php";
    }
  });
  
  
  //disable on select
  $('.rent_type').change(function(){
    if($(this).val() != 2)
    {
      
      //update apartment price
      if($.isNumeric($('#apartment-price').text())  && $(this).val() == 0 )
      {
        var price = $('#daily_price').val();
        $('#apartment-price').text(price);   
      }
      else
      {
        var price = $('#weekly_price').val();
        $('#apartment-price').text(price);   
      }
      
        //update total price
      if($('.period-days').val() != "" && $('.rent_type').val() ==  0)
      {
        period = $('.period-days').val();
        changePrice(price,period)
      }
      else if ($('.period-days').val() != "" && $('.rent_type').val() ==  1)
      {
        period = $('.period-days').val();
        price = price/7;
        changePrice(price,period)
      }
      else
        changePrice(0,0)
      
    }
    else
    {
      var price = $('#monthly_price').val();
      $('#apartment-price').text(price);   
      
      if($('.period-months').val() != "")
      {
        period = $('.period-months').val();
        changePrice(price,period)
      }
      else
        changePrice(0,0)
    }
   
 
  });
  
  
  
  //Increment date by days
  $('.period-days').focusout(function(){
    valu = parseInt($(this).val());
    if($("#from-miladi").val() == "")
    {
      $('#from-miladi').datepicker('setDate', new Date());
    }
    
    $('.period-months').val(Math.floor(valu/30));
    
    var date1 = $('#from-miladi').datepicker('getDate');
    var date = new Date( Date.parse( date1 ) ); 
    date.setDate( date.getDate() + valu );      

    var newDate = date.toDateString(); 
    newDate = new Date( Date.parse( newDate ) );
    
    $('#to-miladi').datepicker('setDate', date);
   
        //change by others part
    var date1 = $('#from-miladi').datepicker('getDate');
    $(this).parents('.add_bookings').find('.show-from-hijri-from').text(writeIslamicDate(date1,1));
    $(this).parents('.add_bookings').find('#show-from-hijri-from').val(writeIslamicDate(date1,1));
    
    var date1 = $('#to-miladi').datepicker('getDate');
    $(this).parents('.add_bookings').find('.show-from-hijri-to').text(writeIslamicDate(date1,1));
    $(this).parents('.add_bookings').find('#show-from-hijri-to').val(writeIslamicDate(date1,1));
     $('.apartment-row').removeAttr("disabled");
    if($.isNumeric($('#apartment-price').text()) && $('.rent_type').val() ==  0)
    {
      price = $('#apartment-price').text();
      changePrice(price,valu);
    }
    else if ($.isNumeric($('#apartment-price').text()) && $('.rent_type').val() ==  1)
    {
      price = $('#apartment-price').text()/7;
      changePrice(price,valu);
    }
    
    else if ($.isNumeric($('#apartment-price').text()) && $('.rent_type').val() ==  2)
    {
      price = $('#apartment-price').text();
      changePrice(price,valu/30);
    }
    else
      changePrice(0,0);
  });
  
  //Increment date by months
  $('.period-months').focusout(function(){
    valu = parseInt($(this).val());
    if($("#from-miladi").val() == "")
    {
      $('#from-miladi').datepicker('setDate', new Date());
    }
    
    $('.period-days').val(Math.floor(valu*30));
    
    var date1 = $('#from-miladi').datepicker('getDate');
    var date = new Date( Date.parse( date1 ) ); 
    date.setMonth( date.getMonth()+valu);      
    var newDate = date.toDateString(); 
    newDate = new Date( Date.parse( newDate ) );
    
    $('#to-miladi').datepicker('setDate', date);
    
    //change by others part
    var date1 = $('#from-miladi').datepicker('getDate');
    $(this).parents('.add_bookings').find('.show-from-hijri-from').text(writeIslamicDate(date1,1));
    $(this).parents('.add_bookings').find('#show-from-hijri-from').val(writeIslamicDate(date1,1));
    
    var date1 = $('#to-miladi').datepicker('getDate');
    $(this).parents('.add_bookings').find('.show-from-hijri-to').text(writeIslamicDate(date1,1));
    $(this).parents('.add_bookings').find('#show-from-hijri-to').val(writeIslamicDate(date1,1));
    $('.apartment-row').removeAttr("disabled");
    if($.isNumeric($('#apartment-price').text())&& $('.rent_type').val() ==  2)
    {
      price = $('#apartment-price').text();
      changePrice(price,valu);
    }
    else if($.isNumeric($('#apartment-price').text())&& $('.rent_type').val() ==  0)
    {
      price = $('#apartment-price').text();
      changePrice(price,valu*30);
    }
    else if($.isNumeric($('#apartment-price').text())&& $('.rent_type').val() ==  1)
    {
      price = $('#apartment-price').text();
      changePrice(price,valu*30/7);
    }
    
  });
  
  //fill hijri on miladi input-change
  $('#from-miladi').change(function(){
    var date1 = $('#from-miladi').datepicker('getDate');
    $(this).parents('.add_bookings').find('.show-from-hijri-from').text(writeIslamicDate(date1,1));
    $(this).parents('.add_bookings').find('#show-from-hijri-from').val(writeIslamicDate(date1,1));
  
  });
  
  //fill hijri on miladi input-change
  $('#to-miladi').change(function(){
    var date1 = $('#to-miladi').datepicker('getDate');
    $(this).parents('.add_bookings').find('.show-from-hijri-to').text(writeIslamicDate(date1,1));
    $(this).parents('.add_bookings').find('#show-from-hijri-to').val(writeIslamicDate(date1,1));
  
  });
 
  $(function() {
    $( "#from" ).datepicker({
      changeMonth: true,
      numberOfMonths: 1,
      dateFormat: 'yy-mm-dd',
      onClose: function( selectedDate ) {
        $( "#to" ).datepicker( "option", "minDate" );
      }
    });
    
    $( "#to" ).datepicker({
      changeMonth: true,
      numberOfMonths: 1,
      dateFormat: 'yy-mm-dd',
      onClose: function( selectedDate ) {
        $( "#from" ).datepicker( "option", "maxDate", selectedDate,$.datepicker.regional[ "" ] );
      }
    });
    
    $( "#from-1" ).datepicker({
      changeMonth: true,
      numberOfMonths: 1,
       dateFormat: 'yy-mm-dd',
      onClose: function( selectedDate ) {
        $( "#to-1" ).datepicker( "option", "minDate" );
      }
    });
    
    $( "#to-1" ).datepicker({
      changeMonth: true,
      numberOfMonths: 1,
      dateFormat: 'yy-mm-dd',
      onClose: function( selectedDate ) {
        $( "#from-1" ).datepicker( "option", "maxDate", selectedDate,$.datepicker.regional[ "" ] );
      }
    });
    
    $( "#from-2" ).datepicker({
      changeMonth: true,
      numberOfMonths: 1,
       dateFormat: 'yy-mm-dd',
      onClose: function( selectedDate ) {
        $( "#to-2" ).datepicker( "option", "minDate" );
      }
    });
    
    $( "#to-2" ).datepicker({
      changeMonth: true,
      numberOfMonths: 1,
       dateFormat: 'yy-mm-dd',
      onClose: function( selectedDate ) {
        $( "#from-2" ).datepicker( "option", "maxDate", selectedDate,$.datepicker.regional[ "" ] );
      }
    });
    
    $( "#from-3" ).datepicker({
      changeMonth: true,
      numberOfMonths: 1,
      dateFormat: 'yy-mm-dd',
      onClose: function( selectedDate ) {
        $( "#to-3" ).datepicker( "option", "minDate" );
      }
    });
    
    $( "#to-3" ).datepicker({
      changeMonth: true,
      numberOfMonths: 1,
       dateFormat: 'yy-mm-dd',
      onClose: function( selectedDate ) {
        $( "#from-3" ).datepicker( "option", "maxDate", selectedDate,$.datepicker.regional[ "" ] );
      }
    });
    
    $( "#from-miladi" ).datepicker({
      defaultDate: "+1w",
      changeMonth: true,
      dateFormat: 'yy-mm-dd',
      numberOfMonths: 1,
      onClose: function( selectedDate ) {
        $( "#to-miladi" ).datepicker( "option", "minDate", selectedDate );
        start = $('#from-miladi').datepicker('getDate');
        end = $('#to-miladi').datepicker('getDate');
        if(end != null)
        {
          var days   = (end - start)/1000/60/60/24;
           var months   = Math.floor(days/30);
           $(this).parent().parent().find('.period-days').val(days);
           $(this).parent().parent().parent().find('.period-months').val(months);
           //get price and period to pass to changePrice function
           if(  $.isNumeric ( $(this).parents('bookings-edit-row').find("#apartment-price").text() ) && $.isNumeric ( $(this).parents('bookings-edit-row').find("#period-days").val() )  ) 
           {
            /*price = $(this).parents('bookings-edit-row').find("#apartment-price").text();
            period = $(this).parents('bookings-edit-row').find("#period-days").val();
            changePrice(price,period);*/
           }
           
           $('.apartment-row').removeAttr("disabled");
        }
        if( $.isNumeric( $(this).parents('.bookings-edit-row').find('.period-days').val() ) )
        {
          period = $(this).parents('.bookings-edit-row').find('.period-days').val();
          
          if($.isNumeric($('#apartment-price').text()) && $('.rent_type').val() ==  0)
          {
            price = $('#apartment-price').text();
            changePrice(price,period);
          }
          else if ($.isNumeric($('#apartment-price').text()) && $('.rent_type').val() ==  1)
          {
            price = $('#apartment-price').text()/7;
            changePrice(price,period);
          }
          
          else if ($.isNumeric($('#apartment-price').text()) && $('.rent_type').val() ==  2)
          {
            price = $('#apartment-price').text();
            changePrice(price,period/30);
          }
          else
            changePrice(0,0);
        }
      }
    });
    
    $( "#to-miladi" ).datepicker({
       defaultDate: "+1w",
      changeMonth: true,
      dateFormat: 'yy-mm-dd',
      numberOfMonths: 1,
      onClose: function( selectedDate ) {
        $( "#from-miladi" ).datepicker( "option", "maxDate", selectedDate );
        end = $('#to-miladi').datepicker('getDate');
        if(start != null)
        {
           var days   = (end - start)/1000/60/60/24;
           var months   = Math.floor(days/30);
           $(this).parent().parent().find('.period-days').val(days);
           $(this).parent().parent().parent().find('.period-months').val(months);
           $('.apartment-row').removeAttr("disabled");
        }
        
        if( $.isNumeric( $(this).parents('.bookings-edit-row').find('.period-days').val() ) )
        {
          period = $(this).parents('.bookings-edit-row').find('.period-days').val();
          
          if($.isNumeric($('#apartment-price').text()) && $('.rent_type').val() ==  0)
          {
            price = $('#apartment-price').text();
            changePrice(price,period);
          }
          else if ($.isNumeric($('#apartment-price').text()) && $('.rent_type').val() ==  1)
          {
            price = $('#apartment-price').text()/7;
            changePrice(price,period);
          }
          
          else if ($.isNumeric($('#apartment-price').text()) && $('.rent_type').val() ==  2)
          {
            price = $('#apartment-price').text();
            changePrice(price,period/30);
          }
          else
            changePrice(0,0);
        }
      },
    });
    
  });
  
   
    
  
  page = $('.currentPage').text();
  elements = $('.totalPages').text();
  total = Math.ceil(elements/20);    
 
  //for searchresults page
  $('#page-selection,#apartments-selection,#contracts-selection,#fur-selection,#payment-selection,#recipt-selection').bootpag({
      total: total,
      leaps: false,
      next: "التالى",
      prev:"السابق",
      page: page
  }).on('page', function(event, num){
      if ($('#page-selection').length ) 
        window.location="customers.php?page="+num;
        
      if ($('#apartments-selection').length ) 
      {
        var data = $('#searchClient').serialize();
        window.location="apartments.php?"+data+"&page="+num;
        
      }
      
      if ($('#contracts-selection').length ) 
      {
       
       $('#page').val(num);
       $('#searchClient').submit();

      }
      
      if ($('#fur-selection').length ) 
      {
        var apart = $('.fur_filter :selected').val();
        window.location="furnitures.php?page="+num+"&apart="+apart;
      }
      
	  if ($('#payment-selection').length ) 
      {
       $('#page').val(num);
       $('#searchClient').submit();
      }
	  if ($('#recipt-selection').length ) 
      {
        $('#page').val(num);
        $('#searchClient').submit();
      }
      
  });
  
  
  $('.fur_filter').change(function(){
   var apart = $('.fur_filter :selected').val();
	 window.location="furnitures.php?apart="+apart;
  });
  
  
  //check if session message is visible, then remove it after 5 seconds
  if ($('.success-message-edit').is(':visible')) {
    $('.success-message-edit').delay(1600);
    $('.success-message-edit').fadeOut(500);
  }
   
   //delete clients/apartments...etc By Sending Ajax request with a filter to delete.php
  $(".delete").click(function(){
    var didConfirm = confirm("هل أنت متأكد من الحذف؟");
    if(didConfirm == true)
    {
      //$('.loading').show();
      var currentObject = $(this);
      var id = $(currentObject).siblings('.delete-id').val();
      var other = $(currentObject).siblings('.other-id').val();
      var filter = $(currentObject).siblings('.filter-id').val();
      window.location="delete.php?other="+other+"&filter="+filter+"&id="+id;
    }
    else
    {
      //do nothing
    }
  
  });

 

  $('.ajax-pop-up').colorbox({
    closeButton:true,
    escKey:false
  });

  //choose apartments and effect its fields on click
  $('.chosen-apartment').live( "click", function() {
    var id=$(this).attr("id");
    var daily_price = $(this).parent().siblings('#daily-rent').text();
    var weekly_price = $(this).parent().siblings('#weekly-rent').text();
    var monthly_price = $(this).parent().siblings('#monthly-rent').text();
    var name = $(this).parent().siblings('#apartment_name').text();
    
    $.colorbox.close();
    
    $('#apartment-found').text(name);
    $('#apartment_id').val(id);
    $('#weekly_price').val(weekly_price);
    $('#daily_price').val(daily_price);
    $('#monthly_price').val(monthly_price);
    
    
    if($('.rent_type').val() == 0)
    {
      var price = daily_price;
    }
    else if($('.rent_type').val() == 1)
    {
      var price = weekly_price;
    }
    else
    {
      var price = monthly_price;
    } 
    
    $('#apartment-price').text(price);
    $('.company-selection').removeAttr("disabled");
    $('.client-selection').removeAttr("disabled");
    
    if($('.period-days').val() != "" && $('.rent_type').val() ==  0)
      period = $('.period-days').val();
    else if ($('.period-days').val() != "" && $('.rent_type').val() ==  1)
    {
      period = $('.period-days').val();
      price = price/7;
    }
    else
      period = $('.period-months').val();
      
    changePrice(price,period);
  });
  
  //choose client and effect its fields on click
  $('.chosen-client').live( "click", function() {
    var id=$(this).attr("id");
    var name=$(this).parent().parent().find('#client-name').text();
    $('#client_id').val(id);
    $.colorbox.close();
    $('.client-found').attr("id",id);
    $('.client-found').text(name);
    $('.company-selection').attr("disabled","disabled");
    $('.discount-control').removeAttr('disabled');
    $('.save-botton').removeAttr('disabled');
    $('.check-in').removeAttr('disabled');
  });
  
  //choose company and effect its fields on click
  $('.chosen-company').live( "click", function() {
    var id=$(this).attr("id");
    var name=$(this).parent().parent().find('#company-name').text();
    $('#company_id').val(id);
    $('#company_name').val(name);
    var discount=parseInt($(this).parent().parent().find('#discount').text());
    $.colorbox.close();
    $('.company-found').attr("id",id);
    $('.company-found').text(name);
    $('.client-selection').attr("disabled","disabled");
    $('.discount').text(discount+"%");
    current = parseInt($('.total_price').text()); 
    current = current - (current*(discount/100));
    $('.total_price,.final_price,.credit').text(current);
    $('#final_price').val(current);
    $('.credit').text("-"+current);
    $('.discount-control').removeAttr('disabled');
    $('.save-botton').removeAttr('disabled');
    $('.check-in').removeAttr('disabled');
  });
  
 
  

  
  $('.get-discount').focusout(function(){
    var valu= $('.get-discount').val();
    var current = $('.total_price').text();
    valu = current - ((valu/100) * current);
    $('.final_price').text(valu);
    $('#final_price').val(valu);
    $('.credit').text("-"+valu);
  });
  
    
  $('.add-pop,.add_client_pop,.view_client_pop,.edit_client_pop,.add_furnitures_pop,.view_furnitures_pop,.edit_furnitures_pop,.link_vouchers_pop,.sms_send_pop,.sms_send_all_pop,.apartments_type').magnificPopup({
		type: 'inline',

		fixedContentPos: false,
		fixedBgPos: true,

		overflowY: 'auto',

		closeBtnInside: false,
    showCloseBtn: false,
		preloader: false,
		
		midClick: true,
		removalDelay: 300,
		mainClass: 'my-mfp-zoom-in'
    
	});
  
  $('.create-booking-pop-up').magnificPopup({
    type: 'inline',

		fixedContentPos: false,
		fixedBgPos: true,

		overflowY: 'auto',
    showCloseBtn:true,
		modal:true,
		preloader: false,
		midClick: true,
		removalDelay: 300,
		mainClass: 'my-mfp-zoom-in'
  });

  
  $('.search-botton').click(function(){
    $('#searchClient').submit();
  })
  
  //send contract form
  $('.save-button').click(function(){
    
  
  });

 

    $(".addAppartment").validate({
      rules: {
        name: "required",
        daily_rent: "required",
        monthly_rent: "required",
        heightest_price: "required"
      },
      
      messages: {
        name: "من فضلك ادخل أسم الوحدة",
        daily_rent: "من فضلك ادخل قيمة الإيجار اليومى",
        monthly_rent: "من فضلك ادخل قيمة الايجار الشهرى",
        heightest_price: "من فضلك أدخل قيمة سعر الذروة"
        
        
      }
    });
	
    // add_furnture validation
	
	$(".add_furn").validate({
      rules: {
        descrip: "required",
        price: "required",
        source: "required",
       
      },
      
      messages: {
        descrip: "تحقق من الوصف",
        price: "تحقق من السعر",
        source: "تحقق من المنشأ",
       
        
        
      }
    });
    
    
    //Login form Validation
    $("#sign-up-form").validate({
      rules: {
        name: "required",
        user_password: "required"
      },
      
      messages: {
        name: "من فضلك أدخل أسم المستخدم",
        user_password: "من فضلك ادخل كلمة المرور"
        
        
      }
    });
    
    //Customer Validation
    $(".validateCustomer").validate({
      rules: {
        name: "required",
        identification_number: "required",
        mobile: "required",
        nationality: "required",
        client_type: "required"
      },
      
      messages: {
        name: "من فضلك ادخل اسم العميل",
        identification_number: "من فضلك ادخل رقم الإثبات",
        mobile: "من فضلك ادخل الجوال",
        nationality: "من فضلك ادخل جنسية العميل",
        client_type: "من فضلك أدخل نوع العميل"
      }
    });
    
    //Company Validation
    $("#add-2").validate({
      messages: {
        name: "من فضلك ادخل اسم الشركة",
        mobile: "من فضلك ادخل هاتف الشركة",
        city: "من فضلك ادخل المدينة",
        representative: "من فضلك ادخل الشخص المسئول",
        discount: "من فضلك ادخل الخصم",
        address: "من فضلك ادخل العنوان",
      }
    });

      
  $('.view_client_pop').click(function(){
    date1 = new Date();
    $('.hijri-date-1').text(writeIslamicDate(date1,1));
    $('#hijri-date-1').val(writeIslamicDate(date1,1));
    var amount = parseFloat($('.final-price').text());
    $('.amout-paid-1').val(amount);
    var service = $('.service_name').val();
    $('#notes').val(service);
  });
  
  $('.save-botton,.add-button,.edit-button,.add-button-2,.small-botton').click(function(){
    $(this).parents('form').submit();
  });
  
   $(".date-1-miladi" ).datepicker({
      changeMonth: true,
      numberOfMonths: 1,
      dateFormat: 'yy-mm-dd',
      onClose: function(  ) {
        var date1 =  $('.date-1-miladi').datepicker('getDate');
          
        $('.hijri-date-1').text(writeIslamicDate(date1,1));
       $('#hijri-date-1').val(writeIslamicDate(date1,1));
      }
    });
  
  $('.check-in').click(function(){
    date1 = $(this).parents('.bookings-edit-container').find('.check-today').datepicker('getDate');
    date2 = new Date();
    date2.setHours(0,0,0,0);
    if(date1.getTime() <= date2.getTime())
    {
      $(this).siblings('#what-button').val(2);
      $(this).parents('form').submit();
    }
    else
    {
      alert("يجب ان يكون تاريخ الدخول مساوياً لتاريخ اليوم او اكبر منه حتى يمكن للعميل دخول الوحدة");
    }
    
  });
  
  $('#contract_status').change(function(){
    $('#searchClient').submit();
    
  });
  
  $('.close-button').click(function(){
    location.reload();
  });
  
  
  $('.choose-service').click(function(){
    var id = $(this).attr('id');
    window.location="addService.php?id="+id;
    $('.selected-service').removeClass('selected-service');
    $(this).addClass('selected-service');
  });
  
  //getUrlParamters
  $.urlParam = function(name){
    var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);
    if (results==null){
       return null;
    }
    else{
       return results[1] || 0;
    }
  }
  /*$('.save-botton').click(function(){
    $('#edit_popup').submit();
  });
  
  $('.add-button').click(function(){
      $('#add').submit();

  });  
  
  $('.edit-button').click(function(){
      $('#edit').submit();

  });

  $('.add-button-2').click(function(){
      $('#add-2').submit();

  });*/
  
  $(".date-miladi" ).datepicker({
      changeMonth: true,
      numberOfMonths: 1,
      dateFormat: 'yy-mm-dd',
      onClose: function(  ) {
        var date1 = $(this).parents("form").find('.date-miladi').datepicker('getDate');
        $(this).parents("form").find('.hijri-date').text(writeIslamicDate(date1,1));
        $(this).parents("form").find('#hijri-date').val(writeIslamicDate(date1,1));
      }
    });
  
  $('#add-2,#periods').validate();
  
  jQuery.extend(jQuery.validator.messages, {
    email: "من فضلك أدخل الايميل بطريقة صحيحة",
    date: "Please enter a valid date.",
  });
    
    $('.edit-price').editable('settings.php', 
    {
     submitdata : function() {
        id= $(this).parent().find('.apart_id').attr("value");
        field = $(this).attr("value");
        return {
          id : id,
          field:field
        };
      },
     indicator : 'Saving...',
     tooltip   : 'Click to edit...'
    });
    
  function changeServicePrice(new_price,arithmtic)
  {
    var discount = 0;
    var price = parseFloat(  $('.total-price').text()  );
    new_price = parseFloat(new_price);

    if(arithmtic == '+')
      price += new_price;
    else
      price -= new_price;
    
    if(  $.isNumeric( $('.discount').val()  )   )
      discount = parseFloat( $('.discount').val() );
    
    $('.total-price').text(price + " ر.س");
    
    //apply discount
    price = price - ((discount/100)*price);
    $('.final-price').text(price + " ر.س");
  }
  
  $('.edit-quantity').editable(function(data) {
    if( $.isNumeric(data) )
    {
      $(this).text(data);
      price = $(this).siblings('.price').text();
      total = price*data;
      $(this).siblings('.total').text(total);
      if ( $(this).parent().find('.chosen-service').is(':checked') ) 
        changeServicePrice(total,'+');
    }
    else
    {
      alert("please enter a valid number");
    }
    
  });
  
  $('.chosen-contract').live( "click", function(){
    contract_id = $(this).parents('.cell-parent').find('#contract_id').text();
    client_id = $(this).parents('.cell-parent').find('#client_id').text();
    amount = parseFloat($('.final-price').text());
    service_id = $.urlParam('id');
    window.location = "addService.php?contract_id="+contract_id+"&client_id="+client_id+"&amount="+amount+"&service="+service_id;
  });
  
  $('.chosen-service').live( "change", function(){
    if (this.checked) 
    {
      var price = parseFloat(  $(this).parent().parent().find('.total').text()   );
      changeServicePrice(price,'+');
    }
    else
    {
      var price = parseFloat(  $(this).parent().parent().find('.total').text()   );
      changeServicePrice(price,'-');
    }
      
   
  });
  
  
  $('.discount').focusout(function(){
    var valu = 0;
    if(  $.isNumeric($('.discount').val() )  )
      valu= $('.discount').val();
      
    var current = parseFloat($('.final-price').text());
    valu = current - ((valu/100) * current);
    $('.final-price').text(valu+ " ر.س");
  });
  
  $('.edit_type').click(function(){
    $(this).siblings('#type_text').hide();
    $(this).siblings('#edit_input').show();
    
  });
  
  $(".button").click(function() {
  $('.edit').show();
  $('.parag').hide();
  });
  
});


